---
apiVersion: v1
kind: Service
metadata:
  name: '{{ ansible_operator_meta.name }}-service'
  namespace: '{{ ansible_operator_meta.namespace }}'
  labels:
    {{ lookup("template", "../common/templates/labels/common.yaml.j2") | indent(width=4) | trim }}
    {{ service_labels | indent(width=4) }}
{% if service_annotations %}
  annotations:
    {{ service_annotations | indent(width=4) }}
{% endif %}
spec:
  ports:
{% if service_type | lower == "nodeport" %}
    - port: 80
      protocol: TCP
      targetPort: 8052
      name: http
{% if nodeport_port is defined %}
      nodePort: {{ nodeport_port }}
{% endif %}
{% elif service_type | lower != 'loadbalancer' and loadbalancer_protocol | lower != 'https' %}
    - port: 80
      protocol: TCP
      targetPort: 8052
      name: http
{% endif %}
{% if ingress_type | lower == 'route' and route_tls_termination_mechanism | lower == 'passthrough' %}
    - port: 443
      protocol: TCP
      targetPort: 8053
      name: https
{% endif %}
{% if service_type | lower == 'loadbalancer' and loadbalancer_protocol | lower == 'https' %}
    - port: {{ loadbalancer_port }}
      protocol: TCP
      targetPort: 8052
      name: https
{% elif service_type | lower == 'loadbalancer' and loadbalancer_protocol | lower != 'https' %}
    - port: {{ loadbalancer_port }}
      protocol: TCP
      targetPort: 8052
      name: http
{% endif %}
  selector:
    app.kubernetes.io/name: '{{ ansible_operator_meta.name }}-web'
    app.kubernetes.io/managed-by: '{{ deployment_type }}-operator'
    app.kubernetes.io/component: '{{ deployment_type }}'
{% if service_type | lower == "nodeport" %}
  type: NodePort
{% elif service_type | lower == "loadbalancer" %}
  type: LoadBalancer
{% if loadbalancer_ip is defined and loadbalancer_ip | length %}
  loadbalancerip: '{{ loadbalancer_ip }}'
{% endif %}
{% if loadbalancer_class is defined and loadbalancer_class | length %}
  loadBalancerClass: {{ loadbalancer_class }}
{% endif %}
{% else %}
  type: ClusterIP
{% endif %}
